package com.j.lemon.learn;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.util.concurrent.ListenableFuture;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

/**
 * @Author lijunjun
 * @Date 2020/9/12 10:38
 * @Description
 */
public class GuavaCache {
    public static Cache<String,String> cache = CacheBuilder.newBuilder().
            removalListener(new CacheExpireListener()).expireAfterWrite(5, TimeUnit.SECONDS).build();

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            cache.put(i+"",i+"");
        }
        new Thread(() -> {
            try {
                while (true){
                    Thread.sleep(1000);
                    ConcurrentMap<String, String> map = cache.asMap();
                    System.out.println(map.size());
                    for (Map.Entry<String, String> entry : map.entrySet()) {
                        System.out.println(entry.getKey()+":"+entry.getValue());
                    }
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
            while(true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                cache.put("0","0");
            }
        }).start();
        Thread.sleep(1000000);




    }
}
